package main

import (
	"energy-amiot-platform/core"
	_ "energy-amiot-platform/docs"
	"energy-amiot-platform/middleware"
	"energy-amiot-platform/routers"
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func initSwagger(r *gin.Engine) {
	enable := viper.GetBool("swagger.enable")
	if enable {
		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	}
}

// @title miot平台开发文档
// @version 0.0.1
// @host wuhan.escitycloud.com:8169
// @BasePath /miot/platform/
// @description push api of push-outer
// @contact.email 3132803373@qq.com
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
	defer func() {
		core.Exit()
	}()

	mode := viper.GetString("http.mode")
	gin.SetMode(mode)
	core.Setup()
	port := viper.GetUint("http.port")

	var engine *gin.Engine
	if mode == gin.DebugMode {
		engine = gin.Default()
		engine.Use(middleware.UseCors())
	} else {
		engine = gin.New()
		engine.Use(middleware.UseSlog(), gin.Recovery(), middleware.UseCors())
	}
	initSwagger(engine)
	routers.UseRouters(engine)

	engine.Run(fmt.Sprintf(":%d", port))
}
